package cn.itcast.service.cargo.impl;

import cn.itcast.dao.product.ProductDao;
import cn.itcast.domain.product.Product;
import cn.itcast.domain.product.ProductExample;
import cn.itcast.service.cargo.ProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

/**
 * 商品列表
 * @author
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    /**
     * 1.新增
     * @param product
     */
    public void save(Product product) {
        product.setId(UUID.randomUUID().toString());
        product.setFactoryName(product.getFactoryName());
        product.setColor(product.getColor());
        product.setDescription(product.getDescription());
        product.setSizeLenght(product.getSizeLenght());
        product.setSizeWidth(product.getSizeWidth());
        product.setSizeHeight(product.getSizeHeight());
        product.setPrice(product.getPrice());
        productDao.insertSelective(product);
    }

    /**
     * 2.删除
     * @param id
     */
    public void delete(String id) {
        productDao.deleteByPrimaryKey(id);
    }

    /**
     * 3.修改
     * @param product
     */
    public void update(Product product) {
        productDao.updateByPrimaryKeySelective(product);
    }

    /**
     * 4.根据id查询
     * @param id
     * @return
     */
    public Product findByID(String id) {
        return productDao.selectByPrimaryKey(id);
    }

    /**
     * 5.分页查询
     * @param example
     * @param page
     * @param size
     * @return
     */
    public PageInfo findPageAll(ProductExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Product> list = productDao.selectByExample(example);
        return new PageInfo(list);
    }

    @Override
    public List<Product> findAll(ProductExample example1) {
        return productDao.selectByExample(example1);
    }


}
